package com.woniuxy.aspect;

import com.woniuxy.entity.Game;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.jetbrains.annotations.NotNull;

//通过注解通知
public class AspectJavaAnnotation {
    //配置内部 切入点表达式
    @Pointcut("execution(* com.woniuxy.service.impl.*.*(..))")
    private void myPointCut(){}
    @Pointcut("execution(* com.woniuxy.service.impl.*.findOne(..))")
    private void myAroundPointCut(){}

    //前置通知
    @Before("myPointCut()")
    public void myBefore(@NotNull JoinPoint joinPoint){
        System.out.println("前置通知：模拟事务的开启……");
        System.out.println("获取目标类："+joinPoint.getTarget());
        System.out.print("，被织入增强的目标方法是："+joinPoint.getSignature().getName());
        System.out.println("…………………………………………………………………………………………");
    }
    //后置通知
    @AfterReturning("myPointCut()")
    public void myAfterReturning(@NotNull JoinPoint joinPoint){
        System.out.println("后置通知：模拟事务的提交……");
        System.out.print("，被织入增强的目标方法是："+joinPoint.getSignature().getName());
        System.out.println("…………………………………………………………………………………………");

    }

    public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知开启：");
        //可以获取入参
        Object[] args = proceedingJoinPoint.getArgs();
        args[0] = 1;
        //反射的执行方法invoke意思
        Game game = (Game) proceedingJoinPoint.proceed(args);
        game.setAccount("嘿嘿嘿，想要查数据……");
        System.out.println("环绕通知结束！");
        return game;
    }
    @AfterThrowing(value = "myPointCut()",throwing ="e" )
    public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("抛出异常："+e.getMessage());
    }
    @After("myPointCut()")
    public void myAfter(){
        System.out.println("最终通知的执行！");
    }
}
